package com.hospital.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hospital.domain.pojo.Department;
import com.hospital.domain.pojo.Doctor;
import com.hospital.domain.utils.Result;
import com.hospital.mapper.DoctorMapper;
import com.hospital.service.DepService;
import com.hospital.service.DoctorService;
import com.hospital.service.UserService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
@RequiredArgsConstructor
public class DoctorServiceImpl extends ServiceImpl<DoctorMapper, Doctor> implements DoctorService {
    private final DepService depService;
    private final UserService userService;
    private final DoctorMapper doctorMapper;

    @Override
    public Result signupDoctor(String name, String sex, Integer age, String title, String major, String info, Integer state, String depName, String pw) {
        Department dep = depService.lambdaQuery().eq(Department::getDepName, depName).one();
        if(dep == null){
            return Result.fail("该科室不存在");
        }
        save(new Doctor(name, sex, age, title, major, info, state, dep.getDepId()));
        Doctor doctor = lambdaQuery().eq(Doctor::getDName, name).one();
        String account = "D" + String.format("%03d", Integer.valueOf(doctor.getDId().toString()));
        return userService.signup(account, pw, 1, 1, doctor.getDId());
    }

    @Override
    public Result queryDoctor(Integer depId) {
        return Result.ok(doctorMapper.queryDoctorByDepId(depId));
    }

    @Override
    public Result queryAllDoctors() {
        List<Map<String, Object>> doctorsWithDept = doctorMapper.queryAllDoctorsWithDepartment();
        return Result.ok(doctorsWithDept);
    }

    @Override
    public Result updateDoctor(Integer dId, String name, String sex, Integer age, String title, String major, String info, Integer state, String depName) {
        Doctor doctor = lambdaQuery().eq(Doctor::getDId, dId).one();
        if(doctor == null){
            return Result.fail("该医生不存在");
        }
        
        Department dep = depService.lambdaQuery().eq(Department::getDepName, depName).one();
        if(dep == null){
            return Result.fail("该科室不存在");
        }
        
        doctor.setDName(name);
        doctor.setDSex(sex);
        doctor.setDAge(age);
        doctor.setDTitle(title);
        doctor.setDMajor(major);
        doctor.setDInfo(info);
        doctor.setDState(state);
        doctor.setDepId(dep.getDepId());
        
        updateById(doctor);
        return Result.ok("医生信息更新成功");
    }

    @Override
    public Result deleteDoctor(Integer dId) {
        Doctor doctor = lambdaQuery().eq(Doctor::getDId, dId).one();
        if(doctor == null){
            return Result.fail("该医生不存在");
        }
        
        // 删除医生对应的用户账号
        userService.lambdaUpdate().eq(com.hospital.domain.pojo.User::getId, dId).remove();
        
        // 删除医生记录
        removeById(dId);
        return Result.ok("医生删除成功");
    }
}
